<?php

namespace App\Http\Controllers;

use App\Http\Requests\IndexShopRequest;
use App\Http\Requests\StoreShopRequest;
use App\Http\Requests\UpdateShopRequest;
use App\Http\Resources\ShopResource;
use App\Models\Admin;
use App\Models\Shop;
use Illuminate\Support\Facades\Auth;
use Spatie\LaravelIgnition\Recorders\DumpRecorder\Dump;

class ShopController extends Controller
{

    public function __construct()
    {
        $this->middleware('auth:sanctum');
    }

    /**
     * Display a listing of the resource.
     */
    public function index(IndexShopRequest $request)
    {
        $this->authorize('viewAny', Shop::class);
        $query = Shop::query();
        if (!isSuperadmin()) {
            $query->where('admin_id', auth()->id());
        }
        $query->when($request->filled('name'), function ($query) use ($request) {
            $name = $request->input('name');
            return $query->where(function ($subquery) use ($name) {
                $subquery->where('name', 'like', "%$name%")
                    ->orWhere('description', 'like', "%$name%");
            });
        })
            ->when($request->filled('price_range'), function ($query) use ($request) {
                $priceRange = explode('-', $request->input('price_range'));
                return $query->whereBetween('price', $priceRange);
            })
            ->when($request->filled('shop_cate_id'), function ($query) use ($request) {
                return $query->where('shop_cate_id', $request->input('shop_cate_id'));
            });

        $data = $query->with(['admins' => function ($query) {
            return $query->select('id', 'username');
        }, 'shopcates' => function ($query) {
            return $query->select('id', 'name');
        }, 'statuslabel', 'norms.normkeys'])->orderBy('created_at', 'DESC')->paginate(
            $request->query('pageSize'),
            ['*'],
            'page',
            $request->query('pageNum')
        );
        $response =  ShopResource::collection($data);
        $total = $response->total();
        return $this->success('查询成功', $response, $total);
    }

    /**
     * Show the form for creating a new resource.
     */
    public function create()
    {
        //
    }

    /**
     * Store a newly created resource in storage.
     */
    public function store(StoreShopRequest $request, Shop $shop)
    {
        $this->authorize('create', $shop);
        $shop->fill($request->input());
        $shop->admin_id = Auth::id();
        $shop->saveOrFail();
        return $this->success('添加商品成功', $shop);
    }

    /**
     * Display the specified resource.
     */
    public function show(Shop $shop)
    {
        $this->authorize('view', $shop);
        $response = $shop->load(['admins', 'shopcates', 'statuslabel', 'norms.normkeys']);
        return $this->success('查询成功', $response);
    }

    /**
     * Show the form for editing the specified resource.
     */
    public function edit(Shop $shop)
    {
        //
    }

    /**
     * Update the specified resource in storage.
     */
    public function update(UpdateShopRequest $request, Shop $shop)
    {
        $this->authorize('update', $shop);
        $shop->fill($request->input());
        $shop->admin_id = Auth::id();
        $shop->saveOrFail();

        return $this->success('更新商品成功', $shop);
    }

    /**
     * Remove the specified resource from storage.
     */
    public function destroy(Shop $shop)
    {
        $this->authorize('delete', $shop);
        $shop->delete();
        return $this->success('删除商品成功', $shop);
    }
}
